package com.bobo.config;


import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.transaction.RabbitTransactionManager;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
@Slf4j
public class RabbitConfig {
    /**
     * 生产者json序列化工具
     * @return
     */
    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    /**
     * 交换机
     * @return
     */
    @Bean
    public DirectExchange orderVoucherExchange() {
        return new DirectExchange("voucher.direct",true,false);  // 持久化交换机
    }

    /**
     * 消息队列正常业务处理
     * @return
     */
    @Bean
    public Queue orderVoucherQueue() {
        return new Queue("voucher.order", true, false, false);
    }
    /**
     * 消费者消息json反序列化工具
     * @param connectionFactory
     * @param messageConverter
     * @return
     */
    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(
            ConnectionFactory connectionFactory, MessageConverter messageConverter) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(messageConverter); // 确保监听器使用JSON转换器
        return factory;
    }

    /**
     * 设置死信交换机的普通队列
     * @return
     */
    @Bean
    public Queue normalQueue(){
        Map<String, Object> arg = new HashMap<>();
        arg.put("x-dead-letter-exchange","voucher.dlx.exchange"); //死信交换机
        arg.put("x-dead-letter-routing-key","voucher.order.dead"); //路由键
        arg.put("x-message-ttl",60000); //TTL 10分钟
        return new Queue("voucher.order.create.queue",true,false,false,arg);
    }
    /**
     * 死信交换机的真实队列
     * @return
     */
    @Bean
    public Queue voucherDLXQueue(){
        return new Queue("voucher.dead.queue");
    }
    /**
     * 死信交换机
     * @return
     */
    @Bean
    public DirectExchange voucherDLXExchange(){
        return new DirectExchange("voucher.dlx.exchange");
    }
    @Bean
    public Binding voucherDLXBinding(){
        return BindingBuilder.bind(voucherDLXQueue()).to(voucherDLXExchange()).with("voucher.order.dead");
    }

    @Bean
    public Binding normalBinding(){
        return BindingBuilder.bind(normalQueue()).to(orderVoucherExchange()).with("voucher.order.delay");
    }

    /**
     * websocket发送异步队列
     * @return
     */
    @Bean
    public Queue wsQueue(){
        return new Queue("order.ws");
    }

    /**
     * websocket异步发送交换机
     * @return
     */
    @Bean
    public Exchange wsExchange(){
        return new DirectExchange("order.direct");
    }
}

